package com.powerbridge.bls.cod.util;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;

/**
 * Base64编码与解码
 */
public class Base64Util {
    /**
     * 对字符串的编码
     */
    public static String encodeString(String srcString) throws Exception {
        String encodeAfter="";
        encodeAfter= Base64.getEncoder().encodeToString(srcString.getBytes("utf-8"));
        return encodeAfter;
    }
    /**
     * 对文件的编码
     */
    public static String encodeFile(File srcFile) throws Exception {
        String encodeAfter="";
        byte[] b = Files.readAllBytes(Paths.get(srcFile.getAbsolutePath()));
        encodeAfter = Base64.getEncoder().encodeToString(b);
        return encodeAfter;
    }
    /**
     * 解码为字符串
     */
    public static String decode2String(String srcString) throws Exception {
        String decodeAfter="";
        decodeAfter=new String(Base64.getDecoder().decode(srcString), "UTF-8");
        return decodeAfter;
    }
    /**
     * 解码为文件写出到指定路径下
     */
    public static void decode2File(String srcString,String write2Path) throws Exception {
        Files.write(Paths.get(write2Path), Base64.getDecoder().decode(srcString),
                StandardOpenOption.CREATE);
    }


}
